package com.baiku.sanhos.service.impl;

import com.baiku.sanhos.domain.LoginUser;
import com.baiku.sanhos.mapper.MenuMapper;
import com.baiku.sanhos.mapper.UserPerMapper;
import com.baiku.sanhos.pojo.entity.User;

import com.baiku.sanhos.utils.security.CustomUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service
public class SmsDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserPerMapper userPerMapper;
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public UserDetails loadUserByUsername(String phone) throws UsernameNotFoundException {

        // 查询用户信息
        String b = phone.substring(0, phone.length()-1); // 截取前11位
        String a = phone.substring(phone.length()-1); // 截取剩余部分
        Integer flag = Integer.valueOf(a);
        Long phone1 = Long.valueOf(b);



        User user = userPerMapper.loadByPhoneFlag(phone1,flag);



        if(Objects.isNull(user)){
            throw  new RuntimeException("用户不存在");
        }
        //进一步归纳角色




        List<String> list = menuMapper.selectPermsByUserId(flag);
        System.out.println(list);

        //TODO 查询对应权限信息

        List<String> list1 = menuMapper.selectPermsByUserId(user.getFlag());



        return new LoginUser(user,list1);
    }
}
